/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package attackDetection;
import java.util.*;

/**
 *
 * @author john
 */
public class Tracer {
    private ArrayList<Plog> plogList;
    private Storage.DBConnect db;
    
    public Tracer(){
        plogList = new ArrayList<Plog>();
        db = new Storage.DBConnect();
    }
    
    public void add(Plog p){
        plogList.add(p);
    }
    
    public void callList(Plog p){
        //Counter
        int matchList = 0;
        int i = 0;
        
        //Internal Parameter
        Plog cplog;
        int nexthopip;
        boolean endList = false;
        
        db.connectToDatabase();
        db.executeQuery("SELECT * FROM log WHERE pseqno=" + p.getPacketSequenceNum() + " AND sip=" 
                + p.getSip() + " AND dip=" + p.getDip(), true);
        
        
    }
    
    public void show(){
        for(int i = 0; i < plogList.size(); i++)
            System.out.println(plogList.get(i));
    }
    
    public void clear(){
        plogList.clear();
    }
    
    public void replayCheck(double hoptime, HashMap<Integer,Integer> numofdelays){
        int i = 0;
        double lb = 0.0;
        double ub = 0.0;
        ub = hoptime;
        double diff1 = 0.0;
        double diff2 = 0.0;
        double diff3 = 0.0;
        boolean foundReplayer = false;
        int replayer = 0;
        Suspicious output;
        
        if(plogList.size() == 1){
            // Missing Implementation
        }
        else if (plogList.size() == 2){
            ub = hoptime;
            diff1 = plogList.get(1).getTimeStamp().getTime() - plogList.get(0).getTimeStamp().getTime();
            
            if(diff1 < lb || diff1 > ub){
                // Missing Implementation
            }
        }
        else if (plogList.size() > 2){
            for(i = 1; i < plogList.size() - 1 && !foundReplayer; i++){
                diff1 = plogList.get(i).getTimeStamp().getTime() - plogList.get(i-1).getTimeStamp().getTime();
                ub = hoptime;
                
                if (diff1 < lb || diff1 > ub) {
                    foundReplayer = true;
                    replayer = plogList.get(i-1).getNodeId();
                    if(numofdelays.containsValue(replayer))
                    numofdelays.put(replayer, numofdelays.get(replayer) + 1);
                }
            }
        }
        
    }

    /**
     * @return the plogList
     */
    public ArrayList<Plog> getPlogList() {
        return plogList;
    }

    /**
     * @param plogList the plogList to set
     */
    public void setPlogList(ArrayList<Plog> plogList) {
        this.plogList = plogList;
    }
    
}
